#include<stdio.h>
struct ListNode {
     int val;
     struct ListNode *next;
 };



struct ListNode* removeElements(struct ListNode* head, int val){
    if(!head)return head;
    struct ListNode *pre=NULL;
    struct ListNode *cur=head;
    while(cur)
    {
        
        if(cur->val==val)
        {
            if(!pre)
            {
                head=cur->next;
                free(cur);
                cur=head;
                continue;
            }
            pre->next=cur->next;
            free(cur);
            cur=pre;
            continue;
        }
        pre=cur;
        cur=cur->next;
    }
    return head;
}